/**
 * 
 * @author jsxgr@qq.com
 * 
 */

 import { Table, Column, Model  } from 'sequelize-typescript';

 @Table({
     tableName:'base_table'
 })
 export default class BaseTable extends Model<BaseTable>{
     @Column({
         primaryKey:true,
         autoIncrement:true
     })
     id:number;

 
     /**
      * 添加
      * @param item 新项目
      */
     static async createItem<T extends BaseTable>(item:T){
         return await this.create(item);
     }
 
     /**
      * 删除
      * @param rid 
      */
     static async deleteById<T extends BaseTable>(id:number){
         return await this.destroy({
             where:{id:id}
         });
     }
 
     /**
      * 更新
      * @param item 新项目对象
      * @param rid 需要修改的项目id
      */
     static async updateItemById<T extends BaseTable>(item:T, id:number){
         const objItem = await this.getById(id) as T;
  
         for(const key in item)
             objItem[key] = item[key];
         return await this.update(objItem, {
         'where': {
             'id': [id]
             }
         });
     }
 
     /**
      * 查询所有
      */
     static async getList<T extends BaseTable>(){
         const items = await this.findAll({raw:true});
         return items as T[];
     }
 
     /**
      * 查询（通过rid）
      * @param rid 
      */
     static async getById<T extends BaseTable>(id:number){
         const item = await this.findOne({
             raw:true,
             where:{id:id}
         });
 
         return item as T;
     }
 
     /**
      * 按条件查询所有
      */
     static async getListByOption<T extends BaseTable>(option:any){
         const items = await this.findAll({
             raw:true,
             where:option
         });
         return items as T[];
     }
 }